home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 038a / bas_int1.zip / FILESPEC.BAS < prev    next >
BASIC Source File  |  1991-03-02  |  3KB  |  92 lines

  1. '======================================================================
  2. ' FILESPEC.BAS by Ronny Ong, April 5, 1988 - 100% Public Domain
  3. '
  4. 'The  sample  program contained in FILESPEC.BAS  demonstrates  how
  5. 'programs  written in QuickBASIC Version 4 or higher and  compiled  and
  6. 'linked to an EXE file can find out the complete drive, path, filename,
  7. 'and  .EXE  extension the program was loaded as.  This  information  is
  8. 'stored in memory when DOS loads a program through its "EXEC" function.
  9. 'The  underlying  approach can be applied to COM or EXE  files  in  any
  10. 'language.
  11. '
  12. ' Example of how to create FILESPEC.EXE:
  13. '   BC FILESPEC/O;
  14. '   LINK /E/NOE FILESPEC,,,QB.LIB;
  15. '=======================================================================
  16.  
  17. '$INCLUDE: 'QB.BI'
  18.  
  19. DIM InRegs AS RegType, OutRegs AS RegType
  20. DIM EnvBlkSeg AS INTEGER, EnvBlkPtr AS INTEGER, Char AS INTEGER
  21. DIM Filespec AS STRING
  22.  
  23. ' The Program Segment Prefix is a 256-byte block which DOS
  24. ' creates below all normal transient programs loaded.  The PSP
  25. ' contains many important pieces of information about the
  26. ' transient program, including the location of its "environment
  27. ' block" in memory.
  28.  
  29. LET InRegs.AX = &H6200 ' Int 21H, Function 62H is Get PSP.
  30. CALL INTERRUPT(&H21, InRegs, OutRegs)
  31. DEF SEG = OutRegs.BX ' Select the segment containing the PSP.
  32.  
  33. ' Get the segment of the environment block, stored at offset 2CH
  34. ' in the PSP.
  35.  
  36. LET EnvBlkSeg = CVI(CHR$(PEEK(&H2C)) + CHR$(PEEK(&H2D)))
  37.  
  38. ' Now select the segment of the environment block itself.
  39. ' Environment blocks are always paragraph-aligned.  That is, they
  40. ' begin only on even 16-byte address boundaries.  Offset 0,
  41. ' therefore, is always the start of the block as long as the
  42. ' segment is set properly.
  43.  
  44. DEF SEG = EnvBlkSeg
  45.  
  46. ' Initialize a pointer to search forward sequentially through
  47. ' memory, looking for the double zero bytes which mark the end of
  48. ' the environment strings.
  49.  
  50. LET EnvBlkPtr = 0
  51.  
  52. DO
  53.   IF PEEK(EnvBlkPtr) = 0 THEN
  54.     IF PEEK(EnvBlkPtr + 1) = 0 THEN
  55.       EXIT DO
  56.     END IF
  57.   END IF
  58.   IF EnvBlkPtr = &H7FFF THEN ' Environment blocks are max of 32K.
  59.     PRINT "End of environment block not found!"
  60.     STOP
  61.   ELSE
  62.     LET EnvBlkPtr = EnvBlkPtr + 1
  63.   END IF
  64. LOOP
  65.  
  66. ' Skip over the double zeroes and the 2-byte word count which
  67. ' precedes the filespec.
  68.  
  69. LET EnvBlkPtr = EnvBlkPtr + 4
  70.  
  71. LET Filespec = "" ' Initialize filespec.
  72.  
  73. ' Assemble Filespec, ensuring that it does not get too long.
  74.  
  75. DO
  76.   LET Char = PEEK(EnvBlkPtr)
  77.   IF Char THEN
  78.     LET Filespec = Filespec + CHR$(Char)
  79.     LET EnvBlkPtr = EnvBlkPtr + 1
  80.   END IF
  81. LOOP WHILE Char > 0 AND LEN(Filespec) < 80
  82.  
  83. ' At this point, Filespec could be used in an OPEN statement to
  84. ' read/write the EXE file, but for this demonstration, it is
  85. ' simply displayed.
  86.  
  87. PRINT "This program was loaded as "; Filespec
  88.  
  89. DEF SEG ' Restore BASIC's default data segment.
  90. END
  91.  
  92.